<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="Plus" @click="handleAdd" v-hasPermi="['system:version:add']">新增</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="versionList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="版本号" align="center" prop="versionNo" />
      <el-table-column label="版本更新描述" align="center" prop="sketch" />
      <el-table-column label="更新类型" align="center" prop="state" />
      <el-table-column label="更新时间" align="center" prop="createDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新包地址" align="center" prop="url" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['system:version:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改app版本管理对话框 -->
    <el-dialog :title="title" v-model="open" width="700px" append-to-body>
      <el-form ref="versionRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="版本号" prop="versionNo">
          <el-input v-model="form.versionNo" placeholder="请输入版本号" />
        </el-form-item>
        <el-form-item label="更新包上传" prop="sketch">
          <FileUpload v-model="form.url" :fileType="['apk', 'wgt']" :fileSize="1000"></FileUpload>
        </el-form-item>
        <el-form-item label="版本更新描述" prop="sketch">
          <el-input v-model="form.sketch" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="更新类型" prop="state">
          <el-radio-group v-model="form.state">
            <el-radio :label="1">整包</el-radio>
            <el-radio :label="0">更新版</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="更新包地址" prop="url">
          <el-input :value="form.url" placeholder="请输入更新包地址" />
        </el-form-item>
        <el-form-item label="是否苹果" prop="isIos">
          <el-switch v-model="form.isIos" :active-value="1" :inactive-value="0" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Version">
import { listVersion, getVersion, delVersion, addVersion, updateVersion } from "@/api/system/version";

const { proxy } = getCurrentInstance();

const versionList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    versionNo: null,
    sketch: null,
    state: null,
    createDate: null,
    applicationId: null,
    url: null,
    isIos: null
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询app版本管理列表 */
function getList() {
  loading.value = true;
  listVersion(queryParams.value).then(response => {
    versionList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    ID: null,
    versionNo: null,
    sketch: null,
    state: null,
    createDate: null,
    applicationId: null,
    url: null,
    isIos: 1
  };
  proxy.resetForm("versionRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.ID);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加app版本管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _ID = row.ID || ids.value
  getVersion(_ID).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改app版本管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["versionRef"].validate(valid => {
    if (valid) {
      if (form.value.ID != null) {
        updateVersion(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addVersion(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _IDs = row.ID || ids.value;
  proxy.$modal.confirm('是否确认删除app版本管理编号为"' + _IDs + '"的数据项？').then(function () {
    return delVersion(_IDs);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/version/export', {
    ...queryParams.value
  }, `version_${new Date().getTime()}.xlsx`)
}

getList();
</script>
